﻿using ComLib.ConfigService;
using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace TopCamera
{
    public class TopCameraOption : IniConfigBase
    {
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        public override string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    string className = this.GetType().Name;
                    _strConfigPath = Path.Combine(ConfigDirectory, className + ".ini");
                }
                return _strConfigPath;
            }
        }

        /// <summary>
        /// PID
        /// </summary>
        private string _pid = "";
        public string Pid
        {
            get { return _pid; }
            set { _pid = value; }
        }

        /// <summary>
        /// VID
        /// </summary>
        private string _vid = "";
        public string Vid
        {
            get { return _vid; }
            set { _vid = value; }
        }

        private string _name = "";
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _width = 0;
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private int _height = 0;
        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("Pid", _pid);
            SaveField("Vid", _vid);
            SaveField("Name", _name);
            SaveField("Width", _width);
            SaveField("Height", _height);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _pid = LoadStringValue("Pid");
                _vid = LoadStringValue("Vid");
                _name = LoadStringValue("Name");
                _width = LoadInt32Value("Width");
                _height = LoadInt32Value("Height");
            }
            catch (Exception ex)
            {
                string strMsg = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("头顶摄像头选项: ");
            builder.Append("[PID]:" + _pid);
            builder.Append("; [VID]:" + _vid);
            builder.Append("; [Name]:" + _name);
            builder.Append("; [Width]:" + _width);
            builder.Append("; [Height]:" + _height);
            return builder.ToString();
        }
    }
}
